using MediatR;
using SmartApplication.Api.Applient.Command.Kill;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Kill
{
    public class UpdateKillHandler : IRequestHandler<UpdateKillCommand, ApiResult<int>>
    {
        public UpdateKillHandler(IBaseRepository<KillModel> killRepository)
        {
            KillRepository = killRepository;
        }

        public IBaseRepository<KillModel> KillRepository { get; }

        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateKillCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var kill = await KillRepository.GetValueAsync(request.Id);
            kill.SourceId = request.SourceId;
            kill.Height = request.Height;
            kill.BeginTime = request.BeginTime;
            kill.EndTime = request.EndTime;
            kill.KillPic = request.KillPic;
            kill.BusinessId = request.BusinessId;
            

            var update = await KillRepository.UpdateAsync(kill);
            if (update > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改屠宰信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改屠宰信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}